package com.bawei.detail.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseExpandableListAdapter
import com.bawei.common.entity.CommendEntity
import com.bawei.common.entity.ReplyCommendEntity
import com.bawei.detail.databinding.ItemCommendChildBinding
import com.bawei.detail.databinding.ItemCommendGroupBinding
import com.bawei.image.ImageLoadUtil
import com.bawei.image.ImageOptions

/**
 * Author: Jason Zhang
 * Date: 2024/9/26
 * Desc:
 */
class CommendListAdapter(private val list: MutableList<CommendEntity>): BaseExpandableListAdapter() {

    override fun getGroupCount(): Int = list.size

    override fun getGroupId(groupPosition: Int): Long = list[groupPosition].id.toLong()

    override fun getGroup(groupPosition: Int): CommendEntity = list[groupPosition]

    override fun getGroupView(groupPosition: Int, isExpanded: Boolean, convertView: View?, parent: ViewGroup?): View {
        val groupEntity = list[groupPosition]
        val groupDataBinding = ItemCommendGroupBinding.inflate(LayoutInflater.from(parent?.context), parent, false)

        ImageLoadUtil.with(groupDataBinding.root.context)
            .setOptions(ImageOptions.Builder.isCircle(true).build())
            .loadUrl(groupEntity.userlogo)
            .into(groupDataBinding.civUserIcon)

        groupDataBinding.tvNickName.text = if(groupEntity.nickname.isNullOrEmpty()) "爱哭的小女孩" else groupEntity.nickname
        groupDataBinding.tvContent.text = groupEntity.content
        groupDataBinding.tvCreateTime.text = "发布时间:${groupEntity.createtime}"
        groupDataBinding.tvExpand.text = if (isExpanded) "——收起回复——" else "——展开回复——"

        return groupDataBinding.root
    }


    override fun getChildrenCount(groupPosition: Int): Int = list[groupPosition].replytotal


    override fun getChild(groupPosition: Int, childPosition: Int): ReplyCommendEntity = list[groupPosition].replyList[childPosition]


    override fun getChildId(groupPosition: Int, childPosition: Int): Long = list[groupPosition].replyList[childPosition].id.toLong()


    override fun getChildView(groupPosition: Int,childPosition: Int, isLastChild: Boolean, convertView: View?, parent: ViewGroup?): View {
        val childEntity = list[groupPosition].replyList[childPosition]
        val childDataBinding = ItemCommendChildBinding.inflate(LayoutInflater.from(parent?.context), parent, false)

        ImageLoadUtil.with(childDataBinding.root.context)
            .setOptions(ImageOptions.Builder.isCircle(true).build())
            .loadUrl(childEntity.userlogo)
            .into(childDataBinding.civUserIcon)

        childDataBinding.tvNickName.text = if(childEntity.nickname.isNullOrEmpty()) "爱哭的小女孩" else childEntity.nickname
        childDataBinding.tvContent.text = childEntity.content
        childDataBinding.tvReplyTime.text = "回复时间:${childEntity.createtime}"
        return childDataBinding.root
    }


    override fun hasStableIds(): Boolean = true


    override fun isChildSelectable(groupPosition: Int, childPosition: Int): Boolean = true
}